package com.team.engine.permission.service;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.team.engine.common.exception.FailedServiceException;
import com.team.engine.common.exception.NullServiceException;
import com.team.engine.common.exception.TypeServiceException;
import com.team.engine.permission.dao.TModule;
import com.team.engine.util.QuickDao;

@Service
@Scope("prototype")
public class PermissionServiceImpl {

	@Resource
	private QuickDao<?, ?> quickDao;

	public void setQuickDao(QuickDao<?, ?> quickDao) {
		this.quickDao = quickDao;
	}
	
	public void addModule(Map<String, String[]> map)
			throws FailedServiceException, TypeServiceException, NullServiceException {
		if (map.size() == 0)
			throw new NullServiceException("请输入模块信息");

		TModule tModule = new TModule();
		
		String[] nameArr = map.get("module_name");
		if(nameArr == null || nameArr[0].trim().length() == 0)
			throw new NullServiceException("模块名称不能是空");
		tModule.setName(nameArr[0]);

		String[] pathArr = map.get("module_path");
		if(pathArr == null || pathArr[0].trim().length() == 0)
			throw new NullServiceException("模块地址不能是空");
		tModule.setPath(pathArr[0]);

		String[] flagArr = map.get("module_flag");
		if (flagArr == null || flagArr[0].trim().length() == 0)
			throw new NullServiceException("模块标识不能是空");
		tModule.setFlag(flagArr[0]);

		String[] parentModuleIdArr = map.get("module_parentModuleId");
		if (parentModuleIdArr != null || parentModuleIdArr[0].trim().length() > 0) {
			String parentModuleId = parentModuleIdArr[0];
			if (!parentModuleId.trim().matches("\\d+"))
				throw new TypeServiceException("父模块Id类型不正确");
			tModule.setParentModuleId(Integer.parseInt(parentModuleId));
		}
		QuickDao<TModule, Integer> dao = (QuickDao<TModule, Integer>) this.quickDao;
		Integer n = dao.getInstance(TModule.class).insert(tModule);
		if (n==null)
			throw new FailedServiceException("数据保存失败");
	}
	
	public static void main(String[] args) {
		System.out.println("03".matches("\\d+"));
	}
}
